//
//  GPSExerciseMapTableViewCell.swift
//  DaFit
//
//  Created by Paul on 2022/10/27.
//  Copyright © 2022 sylar. All rights reserved.
//

import UIKit
import MapKit
import MYModule

open class GPSTrainingMapTableViewCell: UITableViewCell,MKMapViewDelegate {


    open var mapModel: GPSTrainingMapModel? {
        
        didSet {
            
            showMap()
        }
        
    }
    
    public let mapView: MKMapView = MKMapView(frame: CGRect(x: 0, y: 0, width: kScreenWidth, height: 518))
   
    public override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.selectionStyle = .none
        makeUI()
    }
    
    public required init?(coder: NSCoder) {
        super.init(coder: coder)
        makeUI()
    }

    open func makeUI() {
        
        mapView.delegate = self
       
        contentView.addSubview(mapView)
        mapView.snp.makeConstraints { make in
            make.left.right.top.bottom.equalToSuperview()
            make.height.equalTo(518)
        }
        
    }
    
    open func showMap() {
        
        guard let mapModel = mapModel else {
            return
        }
        
        guard let latitude = CLLocationDistance(exactly: mapModel.totlaDistance), latitude > 0 else {
            /// 距离为 0 显示第一个起点位置
            if let coordinate = mapModel.coordiantes.first {
                
                self.mapView.centerCoordinate = coordinate
        
                //设置地图比例
                let region = MKCoordinateRegion(center: coordinate, latitudinalMeters: CLLocationDistance(exactly: 500)!, longitudinalMeters: CLLocationDistance(exactly: 500)!)
                mapView.region = region
                mapView.setRegion(mapView.regionThatFits(region), animated: false)
                
                // 只显示起点
                mapView.addAnnotation(mapModel.startPoint)
                mapView.showAnnotations(mapView.annotations, animated: false)

                
            }
            return
        }
        
        let location = mapView.userLocation
        mapView.centerCoordinate = location.coordinate
        
       // 显示地图比例
        let region = MKCoordinateRegion(center: mapView.centerCoordinate, latitudinalMeters: latitude, longitudinalMeters: latitude)
        mapView.setRegion(mapView.regionThatFits(region), animated: false)
        
        let overlays = self.mapView.overlays
        self.mapView.removeOverlays(overlays)

        let coordiantes = mapModel.coordiantes
        let overlay = MKPolyline(coordinates: coordiantes, count: coordiantes.count)
        mapView.addOverlay(overlay)
        
        mapView.addAnnotation(mapModel.startPoint)
        mapView.addAnnotation(mapModel.endPoint)

        mapView.showAnnotations(mapView.annotations, animated: false)
        

    }
    
    open func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
        if annotation.isKind(of: MKPointAnnotation.self) {
            let pointReuseIndetifier = "pointReuseIndetifier"
            var annotationView: MKAnnotationView? = mapView.dequeueReusableAnnotationView(withIdentifier: pointReuseIndetifier)
            
            if annotationView == nil {
                annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: pointReuseIndetifier)
            }
            if annotation.title == "Start" {
                annotationView!.image = UIImage(named: getCurrentLanguage() == .chinese ? "ic_gps_exercise_map_point_start_zh" : "ic_gps_exercise_map_point_start_en")
            }else if annotation.title == "End"{
                annotationView!.image = UIImage(named: getCurrentLanguage() == .chinese ? "ic_gps_exercise_map_point_end_zh" : "ic_gps_exercise_map_point_end_en")
            }
            
            return annotationView!
        }
        
        return nil
    }
    
    
    /// 绘制多色轨迹
    /// - Parameters:
    ///   - mapView: 地图
    ///   - overlay: 路线
    /// - Returns: 渲染器
    open func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {

        guard let overlay = overlay as? MKPolyline, let colors = mapModel?.colors else { return MKPolylineRenderer(overlay: overlay) }
        
        let polylineRenderer = GradientPathRenderer(polyline: overlay, colors: colors)
        polylineRenderer.lineWidth = 6
        
        return polylineRenderer
        
        
    }
    
}
